<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label=" 代理商用户ID" prop="productLevel">
        <el-input v-model="queryParams.goodName" placeholder="请输入代理商用户ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="代理商用户昵称" prop="goodName">
        <el-input v-model="queryParams.goodName" placeholder="请输入代理商用户昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="" prop="query">
        <el-input
          v-model="queryParams.query"
          placeholder="输入代理商昵称/ID/手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="level">
        <el-select
          v-model="queryParams.level"
          placeholder="请选择代理等级"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="item in dict.type.member_agency_level"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:good:add']">新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <!-- <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:good:export']"
          >导出</el-button
        > -->
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!--  1vip 3共享店主 4联创 5区域运营 6总裁 -->
    <el-table v-loading="loading" :data="goodList">
      <el-table-column label="代理商用户ID" align="center" prop="userId" />
      <el-table-column label="代理商用户昵称" prop="nickName">
        <template slot-scope="{ row }">
          <div style="display: flex; align-items: center">
            <div style="margin-right: 10px">
              <image-preview
                :src="row.avatar"
                width="50px"
                height="50px"
              ></image-preview>
            </div>
            <div>{{ row.nickName }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column label="代理等级" align="center" prop="level">
        <template slot-scope="{ row }">
          <el-tag size="mini" v-if="row.level != 0">{{
            forrmatterEnmu(DictData.memberPurchase, row.level)
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="库存总量" align="center" prop="stockCount" />
      <el-table-column label="操作" align="center" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:good:edit']"
            >详情</el-button
          >
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:good:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px">
      <el-table :data="stockDetailList" height="600px">
        <el-table-column prop="goodName" label="商品名称" width="200">
          <template slot-scope="{ row }">
            <div style="display: flex; align-items: center">
              <div style="margin-right: 10px">
                <image-preview
                  :src="row.goodImg"
                  width="50px"
                  height="50px"
                ></image-preview>
              </div>
              <div>{{ row.goodName }}</div>
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="retailPrice" label="商品售价(元)" />
        <el-table-column prop="stockPrice" label="代理商进货价(元)" />
        <el-table-column prop="profit" label="利润(元)" />
        <el-table-column prop="goodInventory" label="库存数量" />
        <el-table-column label="操作" align="center" fixed="right" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="editHandler(scope.row)"
              v-hasPermi="['system:good:edit']"
              >编辑</el-button
            >
            <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:good:remove']">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 编辑详情 -->
      <el-dialog
        title="编辑代理商商品库存"
        :visible.sync="detailOpen"
        width="800px"
        append-to-body
        @close="$refs['form'].resetFields()"
      >
        <el-table :data="goodsDetailList">
          <el-table-column
            prop="goodId"
            label="商品Id"
            width="80"
            align="center"
          />
          <el-table-column prop="goodName" label="商品信息" width="200">
            <template slot-scope="{ row }">
              <div style="display: flex; align-items: center">
                <div>
                  <image-preview
                    :src="row.avatar"
                    height="50"
                    width="50"
                  ></image-preview>
                </div>
                <div>{{ row.goodName }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="retailPrice"
            label="商品售价(元)"
            align="center"
          />
          <el-table-column
            prop="stockPrice"
            label="代理商进货价(元)"
            align="center"
          />
          <el-table-column prop="profit" label="利润(元)" align="center" />
          <el-table-column
            prop="goodInventory"
            label="库存数量"
            align="center"
          />
        </el-table>
        <el-form
          :model="form"
          ref="form"
          :rules="rules"
          label-width="120px"
          style="margin-top: 20px"
        >
          <el-row>
            <el-col :span="13" :offset="0">
              <el-form-item label="增加/减少库存" prop="inventoryType">
                <el-radio v-model="form.inventoryType" label="1"
                  >增加库存</el-radio
                >
                <el-radio v-model="form.inventoryType" label="2"
                  >减少库存</el-radio
                >
              </el-form-item>
            </el-col>
            <el-col :span="13" :offset="0">
              <el-form-item label="该商品数量增加" prop="goodInventory">
                <el-input v-model="form.goodInventory">
                  <template slot="append">个</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">确定</el-button>
            <el-button @click="detailOpen = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAgencyInventory,
  agencyInventory,
  modifiedInventory,
} from "@/api/inventory/proxy";
import { forrmatterEnmu } from "@/utils/index";
export default {
  name: "Good",
  dicts: ["member_agency_level"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商商品表格数据
      goodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        query: null,
        level: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        inventoryType: [
          {
            required: true,
            message: "商品数量增加不能为空",
            trigger: "blur",
          },
        ],
        goodInventory: [
          {
            required: true,
            message: "增加/减少库存不能为空",
            trigger: "blur",
          },
        ],
      },
      classifyList: [],
      stockDetailList: [],
      goodsDetailList: [],
      detailOpen: false,
      userId: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    forrmatterEnmu,
    /** 查询供应商商品列表 */
    getList() {
      this.loading = true;
      listAgencyInventory(this.queryParams).then((response) => {
        this.goodList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        id: null,
        goodId: null,
        goodInventory: null,
        inventoryType: "1",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 详情按钮操作 */
    handleUpdate(row) {
      this.userId = row.userId;
      this.title = `代理商：${row.nickName} 库存详情`;
      this.getDetailList();
      this.open = true;
    },
    /**获取详情列表 */
    getDetailList() {
      agencyInventory({ userId: this.userId, pageNum: 1, pageSize: 9999 }).then(
        (res) => {
          this.stockDetailList = res.rows;
        }
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return delGood(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/good/export",
        {
          ...this.queryParams,
        },
        `good_${new Date().getTime()}.xlsx`
      );
    },
    /**详情编辑 */
    editHandler(row) {
      let { id, userId, goodId } = row;
      this.form = {
        id,
        userId,
        goodId,
        goodInventory: null,
        inventoryType: "1",
      };
      this.goodsDetailList.splice(0, 1, row);
      this.detailOpen = true;
    },
    onSubmit() {
      this.$refs["form"].validate((v) => {
        if (v) {
          modifiedInventory(this.form).then((res) => {
            this.detailOpen = false;
            this.$message.success("修改成功");
            this.getDetailList();
          });
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.base-title {
  font-size: 18rpx;
  color: #1a1a1a;
  display: flex;
  align-items: center;
  margin-bottom: 15px;

  &::before {
    content: "";
    display: inline-block;
    width: 3px;
    height: 15px;
    background: #1777ff;
    border-radius: 3px;
    margin-right: 8px;
  }
}

::v-deep .el-dialog__header {
  text-align: center;
}
</style>
